package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.dto.RoleDTO;
import cn.edu.sgu.www.pms.entity.Role;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.RoleService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/role", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleController {

    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @ApiOperation("添加角色")
    @RequestMapping(path = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated RoleDTO roleDTO) {
        roleService.insert(roleDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID批量删除角色")
    @RequestMapping(path = "/deleteByIds", method = RequestMethod.POST)
    public JsonResult<Void> deleteByIds(
            @ApiParam(name = "ids", value = "角色ID列表", required = true)
            @Size(min = 1, message = "角色ID列表" + CustomMessages.NOT_NULL)
            @RequestParam("ids") List<Integer> ids) {
        roleService.deleteByIds(ids);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改角色信息")
    @RequestMapping(path = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated RoleDTO roleDTO) {
        roleService.updateById(roleDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID查询角色信息")
    @RequestMapping(path = "/selectById", method = RequestMethod.GET)
    public JsonResult<Role> selectById(
            @ApiParam(name = "id", value = "角色ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        Role role = roleService.selectById(id);

        return JsonResult.success(null, role);
    }

    @ApiOperation("查询角色列表")
    @RequestMapping(path = "/selectList", method = RequestMethod.GET)
    public JsonResult<List<Role>> selectList() {
        List<Role> list = roleService.selectList();

        return JsonResult.success(null, list);
    }

}